/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2010
Version 0.1
=======================================================*/
#include "data/Command.h"

namespace acs
{

Command::Command()
{

}

Command::Command(const int id, const std::string& command, const std::vector<std::string>& arguments)
{
	this->id = id;
	this->command = command;
	this->arguments = arguments;
}

Command::Command(const int id, const int index, const std::string& command, const std::vector<std::string>& arguments)
{
	this->id = id;
	this->index = index;
	this->command = command;
	this->arguments = arguments;
}

Command::~Command()
{

}

void Command::setID(const int id)
{
	this->id = id;
}

int Command::getID() const
{
	return this->id;
}

void Command::setIndex(const unsigned int index)
{
	this->index = index;
}

unsigned int Command::getIndex() const
{
	return this->index;
}

void Command::setCommand(const std::string& command)
{
	this->command=command;
}

std::string Command::getCommand() const
{
	return this->command;
}

void Command::setArguments(const std::vector<std::string>& arguments)
{
	this->arguments = arguments;
}

std::vector<std::string> Command::getArguments() const
{
	return this->arguments;
}

void Command::argumentsFromString(const std::string& args)
{
	rgcpp::StringTokenizer tokenizer(args,' ');

	std::string str;
	this->arguments.clear();
	while(tokenizer.hasMoreElements())
		this->arguments.push_back(rgcpp::Encoder::decode(tokenizer.nextToken()));
}

std::string Command::argumentsToString() const
{
	std::stringstream stream;

	for(std::vector<std::string>::const_iterator itr = arguments.begin(); itr != arguments.end();)
	{
		stream << rgcpp::Encoder::encode(*itr);

		itr++;
		if(itr != arguments.end())
			stream << " ";
	}

	return stream.str();
}

std::string Command::toXML() const
{
	std::stringstream stream;

	stream << "<Command>";
	stream << "<ID>" <<  this->id << "</ID>";
	stream << "<Index>" <<  this->index << "</Index>";
	stream << "<Action>" <<  this->command << "</Action>";
	for(std::vector<std::string>::const_iterator itr = arguments.begin(); itr != arguments.end(); itr++)
		stream << "<Argument>" <<  *itr << "</Argument>";
	stream << "</Command>";

	return stream.str();
}

}
